#include "mystring.h"

char *str_skipws(char *src)
{
    char *pc;
    for (pc = src; *pc && *pc <= 32; ++pc);
    return pc;
}

char *str_skipnws(char *src)
{
    char *pc;
    for (pc = src; *pc > 32; ++pc);
    return pc;
}

char *str_getword(char *src, char *dst)
{
    char *pc;
    for (pc = src; *pc <= 32; ++pc)
        if (!*pc) return NULL;
    char *oc;
    for (oc = dst; *pc > 32; ++pc, ++oc)
        *oc = *pc;
    *oc = 0;
    return pc;
}

char *str_getword(char *src, char *dst, char sep)
{
    char *pc = src;
    if (*pc == sep)
        ++pc;
    if (!*pc)
        return NULL;
    char *oc;
    for (oc = dst; *pc && *pc != sep; ++pc, ++oc)
        *oc = *pc;
    *oc = 0;
    return pc;
}

static const char* glob_S = NULL;

void _Sbegin(const char *s)
{
    glob_S = s;
}

void _Send()
{
    glob_S = NULL;
}

int _S(const char *s)
{
    if (!glob_S) return 0;
    return strcmp(s, glob_S) == 0;
}


void str_filename(char *s_out, const char* s_in, int n)
{
    const char *s1 = strrchr(s_in, '\\');
    const char *s2 = strrchr(s_in, '/');
    const char *ss = (s1 > s2) ? s1 : s2;
    strncpy(s_out, ss+1, n);
}

void str_filepath(char *s_out, const char* s_in, int n)
{
    const char *s1 = strrchr(s_in, '\\');
    const char *s2 = strrchr(s_in, '/');
    const char *ss = (s1 > s2) ? s1 : s2;
    if (!ss) return;
    int nn = ss - s_in;
    strncpy(s_out, s_in, (nn < n) ? nn : n);
}

void str_relpath(char *s_out, const char* s_in, int n)
{
    const char *s1 = strrchr(s_in, '\\');
    const char *s2 = strrchr(s_in, '/');
    const char *ss = (s1 > s2) ? s1 : s2;
    if (!ss) return;
    int nn = ss - s_in;
    strncpy(s_out, s_in, (nn < n) ? nn : n);
}


char *str_n2rn(char* s_out, const char* s_in)
{
    //analyze
    const char *pc;
    int n_cnt = 0;
    for (pc = s_in; *pc; ++pc)
    {
        if (*pc == '\n')
            ++n_cnt;
    }
    //allocate
    int nn = strlen(s_in);
    //char *s_out = malloc(nn + n_cnt + 1);
    //replace
    int i;
    char *cc = s_out;
    for (i = 0; i < nn; ++i)
    {
        if (s_in[i] == '\n')
        {
            *cc = '\r';
            ++cc;
            *cc = '\n';
            ++cc;
        }
        else
        {
            *cc = s_in[i];
            ++cc;
        }
    }
    *cc = 0;
    return s_out;
}
